﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Characteristic_Entity : EntityBase<CHARACTERISTIC>
    {
        public override RESP_Info Add(CHARACTERISTIC model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into CHARACTERISTIC(CHARACTERISTIC_ID,CHARACTERISTIC_NAME,CHARACTERISTIC_DES) values (@CHARACTERISTIC_ID,@CHARACTERISTIC_NAME,@CHARACTERISTIC_DES)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CHARACTERISTIC_ID", model.CHARACTERISTIC_ID));
                    cmd.Parameters.Add(new SqlParameter("CHARACTERISTIC_NAME", model.CHARACTERISTIC_NAME));
                    cmd.Parameters.Add(new SqlParameter("CHARACTERISTIC_DES", model.CHARACTERISTIC_DES));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加行程特色成功</p></h5>";
                    info.Url = "CharacteristicList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加行程特色失败</p></h5>";
                    info.Url = "CharacteristicAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加行程特色失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(CHARACTERISTIC model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update CHARACTERISTIC set CHARACTERISTIC_NAME=@CHARACTERISTIC_NAME,CHARACTERISTIC_DES=@CHARACTERISTIC_DES where CHARACTERISTIC_ID=@CHARACTERISTIC_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CHARACTERISTIC_ID", model.CHARACTERISTIC_ID));
                    cmd.Parameters.Add(new SqlParameter("CHARACTERISTIC_NAME", model.CHARACTERISTIC_NAME));
                    cmd.Parameters.Add(new SqlParameter("CHARACTERISTIC_DES", model.CHARACTERISTIC_DES));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑行程特色成功</p></h5>";
                    info.Url = "CharacteristicList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑行程特色失败</p></h5>";
                    info.Url = "CharacteristicAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑行程特色失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(CHARACTERISTIC route_cs)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from CHARACTERISTIC where CHARACTERISTIC_ID=@CHARACTERISTIC_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CHARACTERISTIC_ID", route_cs.CHARACTERISTIC_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除行程特色成功</p></h5>";
                    info.Url = "CharacteristicList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除行程特色失败</p></h5>";
                    info.Url = "CharacteristicList.aspx";

                    Tom.Net.Log.LogManager.Error("删除行程特色失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override CHARACTERISTIC GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select CHARACTERISTIC_ID,CHARACTERISTIC_NAME,CHARACTERISTIC_DES from CHARACTERISTIC where CHARACTERISTIC_ID=@CHARACTERISTIC_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("CHARACTERISTIC_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        CHARACTERISTIC route_cs = new CHARACTERISTIC();
                        if (!reader.IsDBNull(0)) route_cs.CHARACTERISTIC_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) route_cs.CHARACTERISTIC_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) route_cs.CHARACTERISTIC_DES = reader.GetString(2);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return route_cs;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程特色数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override CHARACTERISTIC GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        CHARACTERISTIC route_cs = new CHARACTERISTIC();
                        if (!reader.IsDBNull(0)) route_cs.CHARACTERISTIC_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) route_cs.CHARACTERISTIC_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) route_cs.CHARACTERISTIC_DES = reader.GetString(2);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return route_cs;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程特色数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<CHARACTERISTIC> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<CHARACTERISTIC> rcs = new List<CHARACTERISTIC>();
                    while (reader.Read())
                    {
                        CHARACTERISTIC route_cs = new CHARACTERISTIC();
                        if (!reader.IsDBNull(0)) route_cs.CHARACTERISTIC_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) route_cs.CHARACTERISTIC_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) route_cs.CHARACTERISTIC_DES = reader.GetString(2);

                        rcs.Add(route_cs);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return rcs;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程特色数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
